package com.rlyy.grpc.client;

import com.alibaba.fastjson.JSONObject;
import com.request.invoke.SendRequestPostProcess;
import com.rlyy.basic.trace.TraceContextHolder;
import com.rlyy.common.param.Request;
import com.rlyy.common.response.Result;
import com.rlyy.grpc.lib.GrpcInterfaceGrpc;
import com.rlyy.grpc.lib.RequestMessage;
import com.rlyy.grpc.lib.ResponseMessage;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * @author yuanqinglong
 * @since 2020/11/26 14:46
 */
@Service
public class GrpcClientServer implements SendRequestPostProcess {


    @GrpcClient(value = "grpc-server")
    private GrpcInterfaceGrpc.GrpcInterfaceBlockingStub grpcStub;



    /**
     * 发送grpc请求
     *
     * @param request 请求信息
     * @return 返回信息
     */
    @Override
    public Result sendRequest(@NonNull final Request request) {
        request.setLinkTrace(TraceContextHolder.getLinkTraceAttributes());
        ResponseMessage handler = grpcStub.handler(RequestMessage.newBuilder().setData(JSONObject.toJSONString(request)).build());
        return JSONObject.parseObject(handler.getData(), Result.class);
    }


}
